package com.box.service.impl;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.box.mapper.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.box.entity.ArticleAndArticleType;
import com.box.entity.ArticleSearch;
import com.box.entity.DbArticle;
import com.box.entity.DbArticleExample;
import com.box.entity.DbArticleExample.Criteria;
import com.box.entity.DbArticletype;
import com.box.entity.DbArticletypeExample;
import com.box.entity.DbUserAndDbArticleResultAndDbArticleType;
import com.box.entity.DbUsers;
import com.box.entity.QueryResultByArticle;
import com.box.entity.QueryType;
import com.box.entity.ViewSearch;
import com.box.entity.isViewSearch;
import com.box.service.ArticleService;
import com.box.utils.ArticleTypePage;
import com.box.utils.ArticleTypePageView;
import com.box.utils.ArticleUtils;
import com.box.utils.GlobalArticle;
import com.box.utils.Globals;
import com.box.utils.LayuiResult;
import com.box.utils.MyUtil;
import com.box.utils.QueryResult;
import com.box.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.reflect.TypeToken;

@Service
public class ArticleServiceImpl implements ArticleService {

	
	
	@Autowired
	private DbArticleMapper articleMapper;	
	
	@Autowired
	private DbArticletypeMapper articletypeMapper;

	
	//获取文章模糊查询列表
	public LayuiResult selArticleList(Integer page, Integer limit,
			ArticleSearch search) {
		
		try {
			
			//分页
			PageHelper.startPage(page,limit);
			DbArticleExample example=new DbArticleExample();
			 example.setOrderByClause("istop DESC");
			Criteria createCriteria = example.createCriteria();
			if(search.getArtitle()!=null&&!search.getArtitle().equals("")){
				//注意：模糊查询需要进行拼接”%“  如下，不进行拼接是不能完成查询的哦。
				createCriteria.andArtitleLike("%"+search.getArtitle()+"%");
				
			}		
			if(search.getSimpledes()!=null&&!search.getSimpledes().equals("")){
				createCriteria.andSimpledesLike("%"+search.getSimpledes()+"%");
			}
			if(search.getIsView()!=null&&!"-1".equals(search.getIsView())){
				Integer isView = Integer.parseInt(search.getIsView());
				createCriteria.andIsviewEqualTo(isView);
				
			}
			if(search.getUid()!=null&&!"-1".equals(search.getUid())){
				Integer uid=Integer.parseInt(search.getUid());
				createCriteria.andUidEqualTo(uid);
			}
			if(search.getArtypeid()!=null&&!"-1".equals(search.getArtypeid())){
				Integer id=Integer.parseInt(search.getArtypeid());
				createCriteria.andArtypeidEqualTo(id);
			}
			if(search.getCreateTimeStart()!=null&&!"".equals(search.getCreateTimeStart())){
				createCriteria.andArcreatetimeGreaterThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeStart()));
			}
			if(search.getCreateTimeEnd()!=null&&!"".equals(search.getCreateTimeEnd())){
			createCriteria.andArcreatetimeLessThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeEnd()));
			}
			List<DbArticle> list = articleMapper.selectByExampleWithBLOBs(example);
			PageInfo<DbArticle> pageinfo=new PageInfo<DbArticle>(list);
			LayuiResult result=new LayuiResult ();
			result.setCode(0);
			result.setCount(pageinfo.getTotal());
			result.setMsg("success");
			result.setData(pageinfo.getList());
			return result;
			
		} catch (Exception e) {
			// TODO: handle exception
			LayuiResult result=new LayuiResult();
			result.setCode(1);
			result.setMsg("fail");
			result.setCount(null);
			result.setData(e);
			return result;
		}
		
		
		
	}
	//待审核未审核列表
	public LayuiResult selArticleListByNotIsView(Integer page, Integer limit,
			isViewSearch search) {
try {
			
			//分页
			PageHelper.startPage(page,limit);
			DbArticleExample example=new DbArticleExample();
			 example.setOrderByClause("isView DESC");
			Criteria createCriteria = example.createCriteria();
			if(search.getArtitle()!=null&&!search.getArtitle().equals("")){
				//注意：模糊查询需要进行拼接”%“  如下，不进行拼接是不能完成查询的哦。
				createCriteria.andArtitleLike("%"+search.getArtitle()+"%");
				
			}			
			if(search.getSimpledes()!=null&&!search.getSimpledes().equals("")){
				createCriteria.andSimpledesLike("%"+search.getSimpledes()+"%");
			}
			if(search.getIsView()!=null&&!"-1".equals(search.getIsView())){
				Integer isView = Integer.parseInt(search.getIsView());
				createCriteria.andIsviewEqualTo(isView);
				
			}
			if(search.getUid()!=null&&!"-1".equals(search.getUid())){
				Integer uid=Integer.parseInt(search.getUid());
				createCriteria.andUidEqualTo(uid);
			}
			if(search.getArtypeid()!=null&&!"-1".equals(search.getArtypeid())){
				Integer id=Integer.parseInt(search.getArtypeid());
				createCriteria.andArtypeidEqualTo(id);
			}
			if(search.getCreateTimeStart()!=null&&!"".equals(search.getCreateTimeStart())){
				createCriteria.andArcreatetimeGreaterThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeStart()));
			}
			if(search.getCreateTimeEnd()!=null&&!"".equals(search.getCreateTimeEnd())){
			createCriteria.andArcreatetimeLessThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeEnd()));
			}
			
			List<DbArticle> list = articleMapper.selectByExampleWithBLOBs(example);
			PageInfo<DbArticle> pageinfo=new PageInfo<DbArticle>(list);
			LayuiResult result=new LayuiResult ();
			result.setCode(0);
			result.setCount(pageinfo.getTotal());
			result.setMsg("success");
			result.setData(pageinfo.getList());
			return result;
			
		} catch (Exception e) {
			// TODO: handle exception
			LayuiResult result=new LayuiResult();
			result.setCode(1);
			result.setMsg("fail");
			result.setCount(null);
			result.setData(e);
			return result;
		}
		
		
		
	}

	
	

	public Integer delArticleById(String arid) {
		// TODO Auto-generated method stub
		Integer n=0;
		try {
		n=	articleMapper.deleteByPrimaryKey(Integer.parseInt(arid));
		System.out.println("成功删除文章记录"+n+"条");
			return n;
		} catch (Exception e) {
			// TODO: handle exception
			return n;
		}
		
	}

	public DbArticle selArticleById(String arid) {
		// TODO Auto-generated method stub
		
		DbArticleExample example=new DbArticleExample();
		Criteria criteria = example.createCriteria();
		criteria .andAridEqualTo(Integer.parseInt(arid));
		DbArticle article  = 	articleMapper.selectByExampleWithBLOBs(example).get(0);
		
		return article;
	}

	public Integer updArticleByStringArticle(DbArticle article) {
		// TODO Auto-generated method stub
		Integer n=0;
		
		try {
			
			article.setUpdatetime(new Date());
			article.setArcreatetime(new Date());
		n=	articleMapper.updateByPrimaryKeySelective(article);
		return n;
		} catch (Exception e) {
			// TODO: handle exception
			return n;
		}
		
		
		
		
		
	}

	public Integer delArticleService(String articleStr) {
		// TODO Auto-generated method stub
		String [] ids=articleStr.split(",");
		Integer n=0;
		if(ids!=null&&ids.length>0){
			for (String arid : ids) {
			n=	articleMapper.deleteByPrimaryKey(Integer.parseInt(arid));
			}
		}
		System.out.println("成功批量删除文章的"+n+"条记录");
		return n;
	}

	public Integer insertArticle(DbArticle article) {
		// TODO Auto-generated method stub
		article.setArcreatetime(new Date());
		article.setUpdatetime(new Date());
		int n = articleMapper.insertSelective(article);
		System.out.println("成功添加了文章记录:"+n);
		return n;
	}
	public LayuiResult updArticleByIsView(String arid) {
		// TODO Auto-generated method stub
		Integer n=3;
		try {
			DbArticleExample example=new DbArticleExample();
			Criteria createCriteria = example.createCriteria();
			createCriteria.andAridEqualTo(Integer.parseInt(arid));
			DbArticle article= articleMapper.selectByExampleWithBLOBs(example).get(0);	
			article.setIsview(1);
			 n = articleMapper.updateByPrimaryKeySelective(article);
			 return  LayuiResult.ok();
		} catch (Exception e) {
			// TODO: handle exception
			
			return new LayuiResult(500, "审核出错");
		}
	
		
		
	
	}
	public LayuiResult delArticleByIsView(String arid) {
		// TODO Auto-generated method stub
		
		try {

		

				Integer n=articleMapper.deleteByPrimaryKey(Integer.parseInt(arid));
			
				return LayuiResult.ok();
			


		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new LayuiResult(500, "单个删除失败");
		}
	
		
	}
	public Integer delArticleByIsViewByAll(String articleStr) {
		// TODO Auto-generated method stub
				String [] ids=articleStr.split(",");
				Integer n=0;
				if(ids!=null&&ids.length>0){
					for (String arid : ids) {
						Integer id = Integer.parseInt(arid);
				
							n=	articleMapper.deleteByPrimaryKey(id);
				
					
					}
				}
				System.out.println("成功批量删除文章的"+n+"条记录");
				return n;
	}
	public LayuiResult selArticleListByIsView(Integer page, Integer limit,
			ViewSearch search) {
try {
			
			//分页
			PageHelper.startPage(page,limit);
			DbArticleExample example=new DbArticleExample();
			 example.setOrderByClause("isView DESC");
			Criteria createCriteria = example.createCriteria();
			if(search.getArtitle()!=null&&!search.getArtitle().equals("")){
				//注意：模糊查询需要进行拼接”%“  如下，不进行拼接是不能完成查询的哦。
				createCriteria.andArtitleLike("%"+search.getArtitle()+"%");
				
			}
			
			if(search.getSimpledes()!=null&&!search.getSimpledes().equals("")){
				createCriteria.andSimpledesLike("%"+search.getSimpledes()+"%");
			}
			if(search.getIsView()!=null&&!"-1".equals(search.getIsView())){
				Integer isView = Integer.parseInt(search.getIsView());
				createCriteria.andIsviewEqualTo(isView);
				
			}
			if(search.getUid()!=null&&!"-1".equals(search.getUid())){
				Integer uid=Integer.parseInt(search.getUid());
				createCriteria.andUidEqualTo(uid);
			}
			if(search.getArtypeid()!=null&&!"-1".equals(search.getArtypeid())){
				Integer id=Integer.parseInt(search.getArtypeid());
				createCriteria.andArtypeidEqualTo(id);
			}
			if(search.getCreateTimeStart()!=null&&!"".equals(search.getCreateTimeStart())){
				createCriteria.andArcreatetimeGreaterThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeStart()));
			}
			if(search.getCreateTimeEnd()!=null&&!"".equals(search.getCreateTimeEnd())){
			createCriteria.andArcreatetimeLessThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeEnd()));
			}
			
			List<DbArticle> list = articleMapper.selectByExampleWithBLOBs(example);
			PageInfo<DbArticle> pageinfo=new PageInfo<DbArticle>(list);
			LayuiResult result=new LayuiResult ();
			result.setCode(0);
			result.setCount(pageinfo.getTotal());
			result.setMsg("success");
			result.setData(pageinfo.getList());
			return result;
			
		} catch (Exception e) {
			// TODO: handle exception
			LayuiResult result=new LayuiResult();
			result.setCode(1);
			result.setMsg("fail");
			result.setCount(null);
			result.setData(e);
			return result;
		}
		
		
	}
	
	/**
	 * 	DbArticleExample example=new DbArticleExample();
					Criteria createCriteria = example.createCriteria();
					createCriteria.andAridEqualTo(Integer.parseInt(arid));
					DbArticle article= articleMapper.selectByExampleWithBLOBs(example).get(0);
	 */
	public LayuiResult updArticleByNotIsView(String arid) {
		// TODO Auto-generated method stub
				Integer n=3;
				try {
					DbArticleExample example=new DbArticleExample();
					Criteria createCriteria = example.createCriteria();
					createCriteria.andAridEqualTo(Integer.parseInt(arid));
					DbArticle article= articleMapper.selectByExampleWithBLOBs(example).get(0);		
					article.setIsview(0);
					 n = articleMapper.updateByPrimaryKeySelective(article);
					 return  LayuiResult.ok();
				} catch (Exception e) {
					// TODO: handle exception
					
					return new LayuiResult(500, "审核出错");
				}
			
	}
	public QueryResult<ArticleAndArticleType> selArticleByPageDeng(Integer artypeid, Integer firstIndex,
			Integer pageNum) {
		// TODO Auto-generated method stub
		List<ArticleAndArticleType> list = articleMapper.selArticleTypeAndArticle(artypeid, firstIndex, pageNum);
		for (ArticleAndArticleType articleAndArticleType : list) {
			System.out.println(articleAndArticleType+"..................查出来的文章列表和文章两表");
		}
		Long count = articleMapper.selCountArticleTypeAndArticle(artypeid);
		
		QueryResult<ArticleAndArticleType> result=new QueryResult<ArticleAndArticleType>();
		result.setList(list);
		result.setTotalRecord(count);
		return result;
	}
	public ArticleTypePage InToThisObjectByArticle(Integer artypeid, Integer page) {
		// TODO Auto-generated method stub(page - 1) * Globals.PAGE_NUM, Globals.PAGE_NUM
		QueryResult<ArticleAndArticleType> query = selArticleByPageDeng(artypeid, (page - 1) *GlobalArticle.PAGE_NUM, GlobalArticle.PAGE_NUM);
		ArticleTypePageView<ArticleAndArticleType> pageview=new ArticleTypePageView<ArticleAndArticleType>(page,(int)query.getTotalRecord(),query.getList());
		Type listType = new TypeToken<ArticleTypePageView<ArticleAndArticleType>>(){}.getType();
		ArticleTypePage result=new ArticleTypePage();
		result.setPageView(pageview);
		result.setListType(listType);
		return result;
	}
	public ArticleAndArticleType selObjectByArid(Integer arid) {
		// TODO Auto-generated method stub
		DbArticle article = articleMapper.selectByPrimaryKey(arid);
		Integer clicks = article.getClicks();
		System.out.println(clicks+".................通过arid查询阅读次数");
		Integer k=++clicks;
		DbArticle a=new DbArticle();
		a.setClicks(k);
		a.setArid(arid);
		articleMapper.updateByPrimaryKeySelective(a);
		
		ArticleAndArticleType selObjectByArticleAndThisType = articleMapper.selObjectByArticleAndThisType(arid);
		System.out.println(selObjectByArticleAndThisType+".........................................查询的数据记录");
		return  selObjectByArticleAndThisType;
	}
	public Integer updateLikeId(Integer arid) {
		// TODO Auto-generated method stub
		DbArticle c = articleMapper.selectByPrimaryKey(arid);
		Integer upperid = c.getUpperid();
		Integer n=++upperid;
		
		DbArticle a=new DbArticle();
		a.setUpperid(n);
		a.setArid(arid);
		Integer cake=articleMapper.updateByPrimaryKeySelective(a);
		return cake;
	}
	public Integer selLikeIdById(Integer arid) {
		// TODO Auto-generated method stub
		DbArticle obj = articleMapper.selectByPrimaryKey(arid);
		return obj.getUpperid();
	}
	public QueryResultByArticle selArticleTypeByTypeId(Integer artypeid) {
		// TODO Auto-generated method stub
		DbArticletypeExample type=new DbArticletypeExample();
		com.box.entity.DbArticletypeExample.Criteria kk = type.createCriteria();
		kk.andArtidEqualTo(artypeid);
		DbArticletype a = articletypeMapper.selectByExample(type).get(0);
		
		DbArticleExample example=new DbArticleExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andArtypeidEqualTo(artypeid);
		List<DbArticle> v = articleMapper.selectByExample(example);
		QueryResultByArticle result=new QueryResultByArticle(a,v);
		return result;
		

	}



}
